home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / Connect-4 Algorithm / game.c < prev   
Encoding:
C/C++ Source or Header  |  1994-11-09  |  7.3 KB  |  230 lines  |  [TEXT/R*ch]

  1. /****************************************************************************/
  2. /**                                                                        **/
  3. /**                          Connect-4 Algorithm                           **/
  4. /**                                                                        **/
  5. /**                            By Keith Pomakis                            **/
  6. /**                     (kppomaki@jeeves.uwaterloo.ca)                     **/
  7. /**                                                                        **/
  8. /**                               Fall, 1993                               **/
  9. /**                                                                        **/
  10. /****************************************************************************/
  11. /**                                                                        **/
  12. /**                          Sample Implementation!                        **/
  13. /**                                                                        **/
  14. /**  This code is poorly written and contains no internal documentation.   **/
  15. /**  Its sole purpose is to quickly demonstrate an actual implementation   **/
  16. /**  of the functions contained in the file "c4.c".  It's a fully working  **/
  17. /**  game which should work on any type of system, so give it a shot!      **/
  18. /**                                                                        **/
  19. /**  The computer is pretty brain-dead at level 3 or less, but at level 4  **/
  20. /**  and up it provides quite a challenge!                                 **/
  21. /**                                                                        **/
  22. /****************************************************************************/
  23.  
  24. #include <stdio.h>
  25. #include <ctype.h>
  26. #include "c4.h"
  27.  
  28. #define MIN(x,y) ((x)<(y)?(x):(y))
  29. #define BUFFER_SIZE 80
  30. #define    EXIT_SUCCESS 0
  31.  
  32. enum {HUMAN, COMPUTER};
  33.  
  34. int get_num(char *prompt, int lower, int upper);
  35. void print_board(int width, int height);
  36. void print_dot(void);
  37.  
  38. static char piece[2] = { 'X', 'O' };
  39.  
  40. main()
  41. {
  42.     int player[2], level[2], turn, num_of_players, move;
  43.     int width, height, num_to_connect;
  44.         long x1, y1, x2, y2;
  45.     char buffer[BUFFER_SIZE];
  46.  
  47.         printf("\n****  Welcome to the game of Connect!  ****\n\n");
  48.         printf("By Keith Pomakis (kppomaki@jeeves.uwaterloo.ca)\n");
  49.         printf("June 16, 1994\n\n");
  50.  
  51.     width = get_num("Width of board (7 is standard)? ", 1, 40);
  52.     height = get_num("Height of board (6 is standard)? ", 1, 40);
  53.     num_to_connect = get_num("Number to connect (4 is standard)? ", 1, 40);
  54.  
  55.     num_of_players = get_num("Number of human players (0, 1 or 2)? ", 0, 2);
  56.     switch (num_of_players) {
  57.  
  58.         case 0:
  59.             player[0] = player[1] = COMPUTER;
  60.             level[0] = get_num("Skill level of player X (1 - 10) ?", 1, 10);
  61.             level[1] = get_num("Skill level of player O (1 - 10) ?", 1, 10);
  62.             poll(print_dot, MIN(level[0], level[1]) - 1);
  63.             turn = 0;
  64.             break;
  65.  
  66.         case 1:
  67.             player[0] = HUMAN;
  68.             player[1] = COMPUTER;
  69.             level[1] = get_num("Skill level of computer (1 - 10) ?", 1, 10);
  70.             poll(print_dot, level[1] - 1);
  71.             buffer[0] = NULL;
  72.             do {
  73.                 printf("Would you like to go first? ");
  74.                 if (!fgets(buffer, BUFFER_SIZE, stdin)) {
  75.                     printf("\nGoodbye!\n");
  76.                     exit(EXIT_SUCCESS);
  77.                 }
  78.                 buffer[0] = tolower(buffer[0]);
  79.             } while (buffer[0] != 'y' && buffer[0] != 'n');
  80.  
  81.             if (buffer[0] == 'y')
  82.                 turn = 0;
  83.             else
  84.                 turn = 1;
  85.             break;
  86.  
  87.         case 2:
  88.             player[0] = player[1] = HUMAN;
  89.             turn = 0;
  90.             break;
  91.     }
  92.  
  93.     new_game(width, height, num_to_connect);
  94.  
  95.     do {
  96.         print_board(width, height);
  97.         if (player[turn] == HUMAN) {
  98.             do {
  99.                 if (num_of_players == 2)
  100.                     sprintf(buffer, "Player %c, drop in which column? ",
  101.                             piece[turn]);
  102.                 else
  103.                     sprintf(buffer, "Drop in which column? ", turn+1);
  104.                 move = get_num(buffer, 1, width) - 1;
  105.             }
  106.             while (!make_move(turn, move));
  107.         }
  108.         else {
  109.             if (num_of_players == 1)
  110.                 printf("Thinking");
  111.             else
  112.                 printf("Player %c is thinking", piece[turn]);
  113.             fflush(stdout);
  114.             move = automatic_move(turn, level[turn]);
  115.             if (num_of_players == 1)
  116.                 printf("\n\nI dropped my piece into column %d.\n", move+1);
  117.             else
  118.                 printf("\n\nPlayer %c dropped its piece into column %d.\n",
  119.                        piece[turn], move+1);
  120.         }
  121.  
  122.         turn = !turn;
  123.  
  124.     } while (!is_winner(0) && !is_winner(1) && !is_tie());
  125.  
  126.     print_board(width, height);
  127.  
  128.     if (is_winner(0)) {
  129.                 win_coords(0, &x1, &y1, &x2, &y2);
  130.         if (num_of_players == 1)
  131.             printf("You won!");
  132.         else
  133.             printf("Player %c won!", piece[0]);
  134.                 printf("  (%d,%d) to (%d,%d)\n\n", x1+1, y1+1, x2+1, y2+1);
  135.     }
  136.     else if (is_winner(1)) {
  137.                 win_coords(1, &x1, &y1, &x2, &y2);
  138.         if (num_of_players == 1)
  139.             printf("I won!");
  140.         else
  141.             printf("Player %c won!", piece[1]);
  142.                 printf("  (%d,%d) to (%d,%d)\n\n", x1+1, y1+1, x2+1, y2+1);
  143.     }
  144.     else {
  145.         printf("There was a tie!\n\n");
  146.     }
  147. }
  148.  
  149.  
  150. /****************************************************************************/
  151.  
  152. int
  153. get_num(char *prompt, int lower, int upper)
  154. {
  155.     int number;
  156.     static char numbuf[40];
  157.  
  158.     do {
  159.         printf("%s", prompt);
  160.         if (!fgets(numbuf, 40, stdin)) {
  161.             printf("\nGoodbye!\n");
  162.             exit(EXIT_SUCCESS);
  163.         }
  164.         sscanf(numbuf, "%d", &number);
  165.     } while (!isdigit(numbuf[0]) || number < lower || number > upper);
  166.  
  167.     return number;
  168. }
  169.  
  170. /****************************************************************************/
  171.  
  172. void
  173. print_board(int width, int height)
  174. {
  175.     Game_state state;
  176.     int x, y;
  177.     char spacing[2], dashing[2];
  178.  
  179.     state = get_game_state();
  180.  
  181.         spacing[1] = dashing[1] = '\0';
  182.     if (width > 19) {
  183.         spacing[0] = '\0';
  184.         dashing[0] = '\0';
  185.     }
  186.     else {
  187.         spacing[0] = ' ';
  188.         dashing[0] = '-';
  189.     }
  190.  
  191.     printf("\n");
  192.     for (y=height-1; y>=0; y--) {
  193.  
  194.         printf("|");
  195.         for (x=0; x<width; x++) {
  196.             if (state.board[x][y] == EMPTY)
  197.                 printf("%s %s|", spacing, spacing);
  198.             else
  199.                 printf("%s%c%s|", spacing, piece[state.board[x][y]], spacing);
  200.         }
  201.         printf("\n");
  202.  
  203.         printf("+");
  204.         for (x=0; x<width; x++)
  205.             printf("%s-%s+", dashing, dashing);
  206.         printf("\n");
  207.     }
  208.  
  209.     printf(" ");
  210.     for (x=0; x<width; x++)
  211.         printf("%s%d%s ", spacing, (x>8)?(x+1)/10:x+1, spacing);
  212.     printf("\n ");
  213.     for (x=0; x<width; x++)
  214.     if (width > 9)
  215.         for (x=0; x<width; x++)
  216.             printf("%s%c%s ", spacing, (x>8)?'0'+(x+1)-((x+1)/10)*10:' ',
  217.                               spacing);
  218.     printf("\n\n");
  219. }
  220.  
  221. /****************************************************************************/
  222.  
  223. void
  224. print_dot(void)
  225. {
  226.     printf(".");
  227.     fflush(stdout);
  228. }
  229.  
  230.